import {Button, Table, message, Popconfirm} from 'antd';
import React, {Component} from "react";
import {DownloadOutlined, WarningOutlined} from "@ant-design/icons";
import {Resultlist, ResultDownload, ResultData, resultDelete} from "@/services/A10/ResultController"
import styles from "@/pages/Model/model.less";
import {getLoginUserUsingGET} from "@/services/a10_back/userController";
// 导入日期处理库
import moment from 'moment';
class ExamplePage extends Component{

  state = {
    resultlist: [],
    listcolumns: [],
    resultcolumns:[],
    Resultdata:[],
    currentPage: 1, // 当前页数
    pageSize: 5, // 每页显示条数
    total: 0, // 总条数
  }
  async componentDidMount() {
    let user=await getLoginUserUsingGET();
    let userid = user.data.id;
    let resultlist = await Resultlist(userid);
    console.log(resultlist.createTime);
    const total = resultlist.length;
    const listcolumns = [
      {
        title: 'id',
        dataIndex: 'id',
        key: 'id',
        colSpan:0,
        render:()=>{return {props:{colSpan:0}};}
      },
      {
        title: 'url',
        dataIndex: 'url',
        key: 'url',
        colSpan:0,
        render:()=>{return {props:{colSpan:0}};}
      },
      {
        title: '分类结果列表',
        dataIndex: 'fileName',
        key: 'name',
        render: (text) => <a>{text}</a>,
      },
      {
        title: '生成时间',
        dataIndex: 'createTime',
        key: 'createTime',
      },
      {
        title: '文件大小',
        dataIndex: 'fileSize',
        key: 'size',
      },
      {
        title: '查看结果',
        dataIndex: 'Download',
        key: 'Download',
        render:(text, record)=> {
          return (<Button type="primary"  onClick={()=>this.handleResultData(record)} className={styles.ButtonInfo}>查看结果</Button>);
        },
      },
      {
        title: '下载',
        key: 'Download',
        dataIndex: 'Download',
        render:(text, record)=> {
          return (<Button type="primary" icon={<DownloadOutlined/>} onClick={()=>this.handResultDownload(record)} className={styles.ButtonInfo}>下载文件</Button>);
        },
      },
      {
        title: '删除',
        key: 'Delete',
        dataIndex: 'Delete',
        render:(text, record)=> {
          return (
            <Popconfirm
              title="确定要删除此文件吗?"
              onConfirm={() => this.handleDeletea(record)}
              icon={
                <WarningOutlined
                  style={{
                    color: 'red',
                  }}
                />
              }
            >
            <Button type="primary" danger className={styles.ButtonInfo}>删除文件</Button>
            </Popconfirm>
          );
        },
      }
    ];
    //分类结果展示
    const resultcolumns = [
      {
        title: '样本ID',
        dataIndex: 'id',
        width: 150,
      },
      {
        title: '分类',
        dataIndex: 'value',
        width: 150,
      }
    ];
    this.setState({resultlist,total});
    this.setState({listcolumns});
    this.setState({resultcolumns});
  }

  handResultDownload(record){
    const key = 'Download';
    message.open({
      key,
      type: 'loading',
      content: '文件下载中...',
      duration: 0
    });
    ResultDownload(record).then(resp =>{
      message.open({
        key,
        type: 'success',
        content: `${record.fileName} 下载完成`,
        duration: 2,
      });
    })
  }

  handleResultData = (record) => {
    console.log(record);
      const { history } = this.props;
      // 跳转到 /list 页面，并携带参数 name = 'example'
      history.push('/Predicted/index?name='+record.fileName+'&url='+record.url);
    };

  loadData = async () => {
    let user = await getLoginUserUsingGET();
    let userid = user.data.id;
    const resultlist = await Resultlist(userid);
    const total = resultlist.length;
    this.setState({ resultlist ,total});
  }
  handleTableChange = (pagination, filters, sorter) => {
    const { current, pageSize } = pagination;
    this.setState({ currentPage: current, pageSize });
  };
  handleDeletea = (record) => {
    resultDelete(record).then((resp) => {
      if(resp.data=="success"){
        message.success("删除文件成功");
      }else {
        message.error("文件删除失败");
      }
      this.loadData();
    });
  }
  render() {
    const { resultlist, listcolumns,currentPage,pageSize,total} = this.state;
    const {config}=this.state;
    const pagination = {
      current: currentPage,
      pageSize,
      total,
      onChange: (page) => {
        this.setState({ currentPage: page });
      },
    };
    return (
      <div>
        <Table columns={listcolumns} dataSource={resultlist} pagination={pagination}></Table>
      </div>
    );
  }
}

export default ExamplePage;

